<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { PropType, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysMenuDetailVo } from '@/api/system/menu/SysMenu/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]
import { selectSysMenuTitleListApi } from '@/api/system/menu/SysMenu'
// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysMenuDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const { formRegister, submit, doWatch } = useBaseWriteView(props)
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]
const platformTypeOptions = ref<Array<KeyLabelValueVo>>([])
const getPlatformType = async () => {
  const res = await sysDictChooseListApi('platformType')
  platformTypeOptions.value = res.data
}
getPlatformType()

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]
const sysMenuTitleList = ref<Array<KeyLabelValueVo>>([])
const getSysMenuTitleList = async () => {
  const res = await selectSysMenuTitleListApi({})
  sysMenuTitleList.value = res.data
}
getSysMenuTitleList()

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'title',
    label: '显示菜单名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入显示菜单名称',
      clearable: true
    }
  },
  {
    field: 'name',
    label: '前端模块名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入前端模块名称',
      clearable: true
    }
  },
  {
    field: 'component',
    label: '前端组件名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入前端组件名称',
      clearable: true
    }
  },
  {
    field: 'parentId',
    label: '父级菜单',
    component: 'Select',
    componentProps: {
      placeholder: '请选择父级菜单ID',
      clearable: true,
      filterable: true,
      options: sysMenuTitleList
    }
  },
  {
    field: 'idPath',
    label: '父级菜单ID路径',
    component: 'Input',
    componentProps: {
      placeholder: '请输入父级菜单ID路径',
      clearable: true,
      disabled: true
    }
  },
  {
    field: 'orderNum',
    label: '排序（越小排越前）',
    value: '100',
    component: 'Input',
    componentProps: {
      placeholder: '请输入排序（越小排越前）',
      clearable: true
    }
  },
  {
    field: 'icon',
    label: '图标',
    component: 'Input',
    componentProps: {
      placeholder: '请输入图标',
      clearable: true
    }
  },
  {
    field: 'hiddenMenu',
    label: '是否隐藏菜单',
    value: false,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择是否隐藏菜单'
    }
  },
  {
    field: 'hiddenTag',
    label: '是否隐藏顶部标签',
    value: false,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择是否隐藏顶部标签'
    }
  },
  {
    field: 'enable',
    label: '是否启用',
    value: true,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择是否启用'
    }
  },
  {
    field: 'canTo',
    label: '是否可跳转',
    value: true,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择是否可跳转'
    }
  },
  {
    field: 'noPermission',
    label: '是否可以无权限跳转',
    value: false,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择是否可以无权限跳转'
    }
  },
  {
    field: 'platformType',
    label: '平台类型',
    value: 'onflow-backend',
    component: 'Select',
    componentProps: {
      placeholder: '请选择平台类型',
      clearable: true,
      filterable: true,
      options: platformTypeOptions
    }
  },
  {
    field: 'remark',
    label: '备注',
    component: 'Input',
    componentProps: {
      placeholder: '请输入备注',
      clearable: true
    }
  },
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建时间',
      disabled: true
    }
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新时间',
      disabled: true
    }
  },
  {
    field: 'createUserName',
    label: '创建人',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建人',
      disabled: true
    }
  },
  {
    field: 'updateUserName',
    label: '更新人',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新人',
      disabled: true
    }
  }
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  title: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('显示菜单名称不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  name: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('前端模块名称不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  component: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 200) {
          callback(new Error('前端组件名称不能超过200个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  idPath: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 2000) {
          callback(new Error('父级菜单ID路径不能超过2000个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  orderNum: [required()],
  icon: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 500) {
          callback(new Error('图标不能超过500个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  hiddenMenu: [required()],
  hiddenTag: [required()],
  enable: [required()],
  canTo: [required()],
  noPermission: [required()],
  platformType: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('平台类型不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  remark: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 500) {
          callback(new Error('备注不能超过500个字符'))
        } else {
          callback()
        }
      }
    }
  ]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
